package ngram_reader.viewer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import main.NGramWorker;
import ngram.exception.NoNGramFilesFoundException;
import ngram.util.IOUtil;
import ngram_reader.viewer.query.NGramSearchQuery;
import swingExtended.EstimatingProgressBar;
import swingExtended.progressPanel.ProgressPanel;
import swingExtended.swingWorkers.ProgressUpdate;

/**
 *
 * @author Shimu
 * @date 10-Nov-2013
 */
public abstract class NGramSearch extends NGramWorker {

        protected final NGramSearchQuery searchQuery;
        private final List<File> INPUT_DIRS;
        
        public NGramSearch(ProgressPanel panel, List<File> inputDirs,
                NGramSearchQuery query, EstimatingProgressBar progressBar) {
            super(panel, progressBar);
            this.searchQuery = query;
            this.INPUT_DIRS = inputDirs;
        }

        protected void publishText(final String text,
                final boolean findCompatibleFont) {
            publish(new ProgressUpdate() {

                @Override
                public void update() {
                    PANEL.getOutputTextPane().setFindCompatibleFont(findCompatibleFont);
                    PANEL.getOutputTextPane().println(text);
                }
            });
        }
        
        @Override
        protected String getHeader() {
            return this.searchQuery.toString();
        }

        @Override
        protected List<String> initFiles() throws IOException, NoNGramFilesFoundException {
            final List<String> files = new ArrayList<>();
            for(File dir : INPUT_DIRS) {
                files.addAll(IOUtil.getFiles(dir));
            }            
            return files;
        }
    }
